package cn.com.jenson.service.main.service.impl;

import cn.com.jenson.common.ImportVerifierVo;
import cn.com.jenson.common.SelectVo;
import cn.com.jenson.entity.main.QSetMeal;
import cn.com.jenson.entity.main.SetMeal;
import cn.com.jenson.exception.BusinessException;
import cn.com.jenson.service.main.repository.SetMealRepository;
import cn.com.jenson.service.main.service.SetMealService;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Resource
    SetMealRepository setMealRepository;

    @Resource
    JPAQueryFactory jpaQueryFactory;

    QSetMeal qSetMeal = QSetMeal.setMeal;

    @Override
    public Page<SetMeal> pageAll(SetMeal setMeal) {
        JPAQuery<SetMeal> jpaQuery = jpaQueryFactory
                .selectFrom(qSetMeal)
                .where(getWhere(setMeal));

        List<SetMeal> list = jpaQuery
                .offset(setMeal.getPageRequest().getOffset())
                .limit(setMeal.getPageRequest().getPageSize())
                .fetch();

        supplement(list);

        return new PageImpl<>(
                list,
                setMeal.getPageRequest(),
                jpaQuery.fetchCount()
        );
    }

    @Override
    public List<SetMeal> listAll(SetMeal setMeal) {
        List<SetMeal> list = jpaQueryFactory
                .selectFrom(qSetMeal)
                .where(getWhere(setMeal))
                .fetch();

        supplement(list);
        return list;
    }

    @Override
    public SetMeal get(SetMeal setMeal) throws InstantiationException, IllegalAccessException {
        SetMeal entity = setMealRepository.findByIdAndDeleted(setMeal.getId(), false)
                .orElseThrow(() -> new BusinessException("未找到id为" + setMeal.getId() + "的套餐"));
        supplement(setMeal);
        return entity;
    }

    @Override
    public BooleanExpression getWhere(SetMeal setMeal) {
        BooleanExpression where = qSetMeal.deleted.eq(false);
        if (Objects.nonNull(setMeal.getSeries())){
            where = where.and(qSetMeal.series.eq(setMeal.getSeries()));
        }
        return where;
    }

    @Override
    public SetMeal edit(SetMeal setMeal) throws InstantiationException, IllegalAccessException {
        setMealRepository.save(setMeal);
        return setMeal;
    }

    @Override
    public List<SetMeal> editAll(List<SetMeal> setMeals) {
        return null;
    }

    @Override
    public SetMeal deleted(SetMeal setMeal) throws InstantiationException, IllegalAccessException {
        SetMeal entity = setMealRepository.findByIdAndDeleted(setMeal.getId(), false)
                .orElseThrow(() -> new BusinessException("未找到id为" + setMeal.getId() + "的套餐"));
        entity.setDeleted(true);
        setMealRepository.save(entity);
        return entity;
    }

    @Override
    public List<SetMeal> deletedAll(List<SetMeal> setMeals) {
        return null;
    }

    @Override
    public List<ImportVerifierVo> importTemplateVerifiers() {
        return null;
    }

    @Override
    public void importTemplateDownload(HttpServletResponse response) throws IOException {

    }

    @Override
    public List<SelectVo<String, Integer, SetMeal>> option() {
        return null;
    }

    @Override
    public void supplement(SetMeal setMeal) {

    }

    @Override
    public void supplement(List<SetMeal> setMeals) {

    }

    @Override
    public void supplement(Page<SetMeal> setMeals) {

    }
}
